<html>
<head><meta charset="utf-8"><title>macro lowering issue · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macro.20lowering.20issue.html">macro lowering issue</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="196074662"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macro%20lowering%20issue/near/196074662" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macro.20lowering.20issue.html#196074662">(May 02 2020 at 23:18)</a>:</h4>
<p>Hello everyone! I am trying to investigate an issue that I reported <a href="https://github.com/rust-lang/rust/issues/71104" title="https://github.com/rust-lang/rust/issues/71104">https://github.com/rust-lang/rust/issues/71104</a><br>
And _i think_ that declarative macro 2.0 in closure are not lowered to hir properly. Looking at the <a href="https://github.com/rust-lang/rust/blob/master/src/test/rustdoc/macro-in-closure.rs" title="https://github.com/rust-lang/rust/blob/master/src/test/rustdoc/macro-in-closure.rs">https://github.com/rust-lang/rust/blob/master/src/test/rustdoc/macro-in-closure.rs</a> test case, i end up in a situation where the def id does not have a corresponding hir id</p>



<a name="196074663"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macro%20lowering%20issue/near/196074663" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macro.20lowering.20issue.html#196074663">(May 02 2020 at 23:18)</a>:</h4>
<p>Does that seem sensible?</p>



<a name="196076796"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macro%20lowering%20issue/near/196076796" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macro.20lowering.20issue.html#196076796">(May 03 2020 at 00:27)</a>:</h4>
<p>so the bug is triggered because the declarative macro is appended to the <code>exported_macros</code> vec and later on rustdoc tries to access the parent def id of the macro, which is not properly lowered because its a closure</p>



<a name="196076998"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macro%20lowering%20issue/near/196076998" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macro.20lowering.20issue.html#196076998">(May 03 2020 at 00:33)</a>:</h4>
<p>should a declarative macro be exported when defined in a closure?</p>



<a name="196078758"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macro%20lowering%20issue/near/196078758" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macro.20lowering.20issue.html#196078758">(May 03 2020 at 01:26)</a>:</h4>
<p>I filled a bug for this in case I forget: <a href="https://github.com/rust-lang/rust/issues/71820" title="https://github.com/rust-lang/rust/issues/71820">https://github.com/rust-lang/rust/issues/71820</a></p>



<a name="196117526"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macro%20lowering%20issue/near/196117526" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macro.20lowering.20issue.html#196117526">(May 03 2020 at 19:04)</a>:</h4>
<p>I'd like to fix this issue to understand the internals of the compiler, would someone be ready to mentor me? I have a few questions</p>



<a name="196118550"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macro%20lowering%20issue/near/196118550" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macro.20lowering.20issue.html#196118550">(May 03 2020 at 19:30)</a>:</h4>
<p><span class="user-mention" data-user-id="123856">@Vadim Petrochenkov</span> if you have some time :) its about lowering of declararive macro</p>



<a name="196175763"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macro%20lowering%20issue/near/196175763" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macro.20lowering.20issue.html#196175763">(May 04 2020 at 12:53)</a>:</h4>
<p>Not sure about the details, but my first question here is what happens with non-macro items?</p>



<a name="196175797"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macro%20lowering%20issue/near/196175797" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macro.20lowering.20issue.html#196175797">(May 04 2020 at 12:53)</a>:</h4>
<p>(Non-macro items if they are defined in a closure.)</p>



<a name="196175852"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macro%20lowering%20issue/near/196175852" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macro.20lowering.20issue.html#196175852">(May 04 2020 at 12:54)</a>:</h4>
<p>Macro items should then behave identically.</p>



<a name="196176053"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macro%20lowering%20issue/near/196176053" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macro.20lowering.20issue.html#196176053">(May 04 2020 at 12:56)</a>:</h4>
<p>In general, I'm not sure why macros are lowered into a separate list in HIR.</p>



<a name="196176126"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macro%20lowering%20issue/near/196176126" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macro.20lowering.20issue.html#196176126">(May 04 2020 at 12:57)</a>:</h4>
<p>Perhaps then need to be lowered into regular HIR items since they need to exist until metadata encoding and metadata encoding is performed on HIR.</p>



<a name="196176238"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macro%20lowering%20issue/near/196176238" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macro.20lowering.20issue.html#196176238">(May 04 2020 at 12:58)</a>:</h4>
<p>Anyway, doing that is probably not necessary for resolving the issue with IDs, it's likely that macros can do same things with IDs like other items while still being stored in a separate list.</p>



<a name="196215775"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macro%20lowering%20issue/near/196215775" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macro.20lowering.20issue.html#196215775">(May 04 2020 at 17:49)</a>:</h4>
<p><span class="user-mention" data-user-id="123856">@Vadim Petrochenkov</span> ok ill try to understand how it works for other items. On the same topic, i see that now declarative macros have a visibility token associated pub/pub(crate)/etc. Should a macro without pub/pub(crate) inside a closure be exported (ie: made public)?</p>



<a name="196216245"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macro%20lowering%20issue/near/196216245" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macro.20lowering.20issue.html#196216245">(May 04 2020 at 17:53)</a>:</h4>
<p>Same logic "macro items should behave like other items" applies here.</p>



<a name="196216382"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macro%20lowering%20issue/near/196216382" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macro.20lowering.20issue.html#196216382">(May 04 2020 at 17:54)</a>:</h4>
<p>We encode all items, even private ones.<br>
They are used in diagnostics, at the very least.</p>



<a name="196216858"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macro%20lowering%20issue/near/196216858" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macro.20lowering.20issue.html#196216858">(May 04 2020 at 17:58)</a>:</h4>
<p>I checked for regular macro rules, and if the macro_export attribute is not present, they are not part of the hir tree</p>



<a name="196245426"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macro%20lowering%20issue/near/196245426" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macro.20lowering.20issue.html#196245426">(May 04 2020 at 22:04)</a>:</h4>
<p>I managed to reproduce it using regular macro rules so i guess its a more generic issue</p>



<a name="196251329"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macro%20lowering%20issue/near/196251329" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macro.20lowering.20issue.html#196251329">(May 04 2020 at 23:15)</a>:</h4>
<p>Is this really supposed to be working: <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=b0a0cf8828275c5be13a89d96df1b19b" title="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=b0a0cf8828275c5be13a89d96df1b19b">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=b0a0cf8828275c5be13a89d96df1b19b</a> ??</p>



<a name="196251356"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macro%20lowering%20issue/near/196251356" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macro.20lowering.20issue.html#196251356">(May 04 2020 at 23:15)</a>:</h4>
<p>#[macro_export] exposes the macro to the whole current module/crate? No matter where the macro is defined?</p>



<a name="196253239"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macro%20lowering%20issue/near/196253239" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macro.20lowering.20issue.html#196253239">(May 04 2020 at 23:44)</a>:</h4>
<p>Yep, that's how it works.<br>
<code>#[macro_export]</code> plants the macro into the crate root, so it's available as <code>that_crate::mac</code> to other crates and as <code>crate::mac</code> to this crate.</p>



<a name="196253281"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macro%20lowering%20issue/near/196253281" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macro.20lowering.20issue.html#196253281">(May 04 2020 at 23:45)</a>:</h4>
<p>Compatibility with macros 1.0.</p>



<a name="196253409"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macro%20lowering%20issue/near/196253409" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macro.20lowering.20issue.html#196253409">(May 04 2020 at 23:47)</a>:</h4>
<p>Ok</p>



<a name="196253482"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macro%20lowering%20issue/near/196253482" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macro.20lowering.20issue.html#196253482">(May 04 2020 at 23:48)</a>:</h4>
<p>and in term of hir, i tried to understand how other thing works when within a closure but macros are kind of the only thing that can be defined inside a closure and be visible outside</p>



<a name="196253597"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macro%20lowering%20issue/near/196253597" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macro.20lowering.20issue.html#196253597">(May 04 2020 at 23:50)</a>:</h4>
<p>so i don't know how in that case they should be lowered, basically i don't know if 1/ the closure should be lowered differently or 2/ the exported macros should belong to the module directly</p>



<a name="196987500"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macro%20lowering%20issue/near/196987500" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macro.20lowering.20issue.html#196987500">(May 09 2020 at 11:38)</a>:</h4>
<p>I tried to avoid the bug (not really fix it) by calling <code>get_parent_did</code> instead of <code>DefIdTree::parent</code> to get the parent hir_id: <a href="https://github.com/rust-lang/rust/pull/72039" title="https://github.com/rust-lang/rust/pull/72039">https://github.com/rust-lang/rust/pull/72039</a><br>
But now it fails in other tests with a </p>
<div class="codehilite"><pre><span></span><code>error: internal compiler error: src/librustc_mir/monomorphize/collector.rs:761: cannot create local mono-item for DefId(15:5 ~ nested_fn_macro[8787]::n[0]::p[0]::f[0])
</code></pre></div>



<a name="196987756"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/macro%20lowering%20issue/near/196987756" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/macro.20lowering.20issue.html#196987756">(May 09 2020 at 11:44)</a>:</h4>
<p>If anyone with experiences around macro and lowering has some spare time, i could really use some help :)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>